package com.xgst.land.viewmodel

import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.xgst.land.base.BaseViewModel
import com.xgst.land.bean.CheckLocationBean
import com.xgst.land.bean.SearchUidNameBean
import com.xgst.land.bean.SuccessBean
import com.xgst.land.http.api.CheckReplacePI
import com.xgst.land.http.api.PDAPI
import com.xgst.land.http.api.PDSubmitAPI
import java.lang.Exception

class PDScanViewModel: BaseViewModel() {
    fun scanData(uid:String, orderId:String, onResult:(CheckLocationBean.DataBean)->Unit){
        EasyHttp.post(this)
            .api(PDAPI(orderId,uid))
            .request(object : OnHttpListener<CheckLocationBean>{
                override fun onSucceed(result: CheckLocationBean) {
                    onResult(result.data)
                }

                override fun onFail(e: Exception) {
                    dismissProgress(e.message?:"盘点数据获取失败")
                }
            })
    }

    fun submit(boxUID: String, onResult: () -> Unit, onError: (String) -> Unit) {
        EasyHttp.post(this)
            .api(PDSubmitAPI("1",boxUID))
            .request(object : OnHttpListener<SuccessBean>{
                override fun onSucceed(result: SuccessBean) {
                    onResult()
                }

                override fun onFail(e: Exception) {
                    dismissProgress(e.message?:"提交失败")
                }
            })
    }

    /**
     * 查询uid对应的名字
     */
    fun searchName(uid:MutableList<String>, onResult:(MutableList<String>)->Unit, onError:(String)->Unit){
        EasyHttp.post(this)
            .api("other/app_rfidSample/querySampleInfoListByUids.do")
            .json(uid)
            .request(object : OnHttpListener<SearchUidNameBean> {
                override fun onSucceed(result: SearchUidNameBean) {
                    val list = mutableListOf<String>()
                    result.data.sampleInfoList.forEach {
                        list += it.name
                    }
                    onResult(list)
                }

                override fun onFail(e: Exception) {
                    dismissProgress(e.message?:"")
                }
            })
    }

    //替换
    fun replace(oldUID:String,newUID:String,onSuccess:()->Unit,onError: (String) -> Unit){
        EasyHttp.post(this)
            .api(CheckReplacePI(oldUID,newUID))
            .request(object : OnHttpListener<SuccessBean>{
                override fun onSucceed(result: SuccessBean?) {
                    onSuccess()
                }

                override fun onFail(e: Exception) {
                    onError(e.message?:"")
                }
            })
    }
}
